{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'results': [{'address_components': [{'long_name': 'Google Building 41',\n",
      "                                      'short_name': 'Google Bldg 41',\n",
      "                                      'types': ['premise']},\n",
      "                                     {'long_name': '1600',\n",
      "                                      'short_name': '1600',\n",
      "                                      'types': ['street_number']},\n",
      "                                     {'long_name': 'Amphitheatre Parkway',\n",
      "                                      'short_name': 'Amphitheatre Pkwy',\n",
      "                                      'types': ['route']},\n",
      "                                     {'long_name': 'Mountain View',\n",
      "                                      'short_name': 'Mountain View',\n",
      "                                      'types': ['locality', 'political']},\n",
      "                                     {'long_name': 'Santa Clara County',\n",
      "                                      'short_name': 'Santa Clara County',\n",
      "                                      'types': ['administrative_area_level_2',\n",
      "                                                'political']},\n",
      "                                     {'long_name': 'California',\n",
      "                                      'short_name': 'CA',\n",
      "                                      'types': ['administrative_area_level_1',\n",
      "                                                'political']},\n",
      "                                     {'long_name': 'United States',\n",
      "                                      'short_name': 'US',\n",
      "                                      'types': ['country', 'political']},\n",
      "                                     {'long_name': '94043',\n",
      "                                      'short_name': '94043',\n",
      "                                      'types': ['postal_code']}],\n",
      "              'formatted_address': 'Google Bldg 41, 1600 Amphitheatre Pkwy, '\n",
      "                                   'Mountain View, CA 94043, USA',\n",
      "              'geometry': {'bounds': {'northeast': {'lat': 37.4228642,\n",
      "                                                    'lng': -122.0851557},\n",
      "                                      'southwest': {'lat': 37.4221145,\n",
      "                                                    'lng': -122.0859841}},\n",
      "                           'location': {'lat': 37.4224082, 'lng': -122.0856086},\n",
      "                           'location_type': 'ROOFTOP',\n",
      "                           'viewport': {'northeast': {'lat': 37.4238383302915,\n",
      "                                                      'lng': -122.0842209197085},\n",
      "                                        'southwest': {'lat': 37.4211403697085,\n",
      "                                                      'lng': -122.0869188802915}}},\n",
      "              'place_id': 'ChIJxQvW8wK6j4AR3ukttGy3w2s',\n",
      "              'types': ['premise']}],\n",
      " 'status': 'OK'}\n"
     ]
    }
   ],
   "source": [
    "from urllib.request import urlopen\n",
    "from urllib.parse import quote_plus\n",
    "import json\n",
    "\n",
    "address = '1600+Amphitheatre+Parkway,+Mountain+View,+CA'\n",
    "u = urlopen('https://maps.googleapis.com/maps/api/geocode/json?address=%s'% quote_plus(address))\n",
    "resp = json.loads(u.read().decode('utf-8'))\n",
    "from pprint import pprint\n",
    "pprint(resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-122.0856086"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resp['results'][0]['geometry']['location']['lat']\n",
    "resp['results'][0]['geometry']['location']['lng']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
      "  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',\n",
      "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
      "  tol=0.001, verbose=False)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import svm\n",
    "X = [[1, 0, 1], [-1, 0, -1]]\n",
    "y = [1, -1]\n",
    "clf = svm.SVC()\n",
    "print(clf.fit(X, y))\n",
    "clf.predict([[1, 1, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
      "  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',\n",
      "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
      "  tol=0.001, verbose=False)\n"
     ]
    }
   ],
   "source": [
    "import advancedclassify\n",
    "from imp import reload\n",
    "reload(advancedclassify)\n",
    "numericalset = advancedclassify.loadnumerical()\n",
    "scaledset, scalef = advancedclassify.scaledata(numericalset)\n",
    "\n",
    "answers, input = [r.match for r in scaledset], [r.data for r in scaledset]\n",
    "clf = svm.SVC()\n",
    "print(clf.fit(input, answers))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.predict([scalef([28, -1, 1, 26, -1, 1, 2, 0])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(input, answers, test_size=0.7, random_state=0)\n",
    "clf = svm.SVC().fit(X_train, y_train)\n",
    "print(clf.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.84 (+/- 0.20)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "clf = svm.SVC()\n",
    "scores = cross_val_score(clf, input, answers, cv=50)\n",
    "print(\"Accuracy: %0.2f (+/- %0.2f)\" % (scores.mean(), scores.std() * 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
